package de.sendorian.app.forumArchive.dundjinni;


import static de.sendorian.app.forumArchive.matcher.ForumMatcher.*;
import static de.sendorian.app.forumArchive.renamer.Renamers.*;
import static org.hamcrest.Matchers.*;

import java.text.SimpleDateFormat;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import org.hamcrest.Matcher;
import org.joda.time.DateTime;

import de.sendorian.app.forumArchive.ConfigStrategy;
import de.sendorian.app.forumArchive.domain.Forum;
import de.sendorian.app.forumArchive.domain.Post;
import de.sendorian.app.forumArchive.domain.Topic;
import de.sendorian.app.forumArchive.finder.Finder;
import de.sendorian.app.forumArchive.renamer.FileRenamer;

public class DundjinniConfigStrategy implements ConfigStrategy {

	private static Logger logger = Logger.getLogger(DundjinniConfigStrategy.class.getName());

	private static final String BASE_URL = "http://www.dundjinni.com/forums/";

	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy h:mma");

	private Pattern DATE_PATTERN = Pattern
	    .compile("(\\d{2}/\\d{2}/\\d{4}).*at.*(\\d{1,2}:\\d{2}.m)");

	private Properties properties;

	private DateTime startDate;

	private DateTime endDate;


	public String getBaseUrl() {
		return BASE_URL;
	}


	public SimpleDateFormat getDateFormat() {
		return DATE_FORMAT;
	}


	public Pattern getDatePattern() {
		return DATE_PATTERN;
	}


	public Finder<String, Forum> getForumFinder() {
		return new DundjinniForumFinder();
	}


	public Finder<Topic, Post> getPostFinder() {
		return new DundjinniPostFinder();
	}


	public Finder<Forum, Topic> getTopicFinder() {
		return new DundjinniTopicFinder();
	}


	public Matcher<Post> getPostMatcher() {
		if (getStartDate() != null && getEndDate() != null) {
			return matchesDateInterval(getStartDate(), getEndDate());
		}
		return anything();
	}


	public Matcher<Topic> getTopicMatcher() {
		if (getStartDate() != null) {
			return matchesDateIntervalUntilNow(getStartDate());
		}
		return anything();
	}


	public FileRenamer getDefaultRenamer() {
		return both(removeHash()).and(capitalizeFirst()).and(appendUserShortname());
	}


	public void setProperties(Properties properties) {
		this.properties = properties;
	}


	private DateTime getStartDate() {
		if (startDate == null) {
			try {
				startDate = new DateTime(properties.getProperty("start.date"));
				logger.info("Configured start date: " + startDate);
			} catch (Exception e) {
				logger.log(Level.WARNING,
				    "Could not parse start.date property or property does not exist.", e);
			}
		}
		return startDate;
	}


	private DateTime getEndDate() {
		if (endDate == null) {
			try {
				endDate = new DateTime(properties.getProperty("end.date"));
				logger.info("Configured start date: " + endDate);
			} catch (Exception e) {
				logger.log(Level.WARNING,
				    "Could not parse end.date property or property does not exist.", e);
			}
		}
		return endDate;
	}

}
